<?php
namespace app\model\mysql;

use think\Model;

class User extends Model {
    protected $name = "user";
    /**
     *  自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 创建时间字段 false表示关闭
     * @var false|string
     */
    protected $createTime = 'created_time';

    /**
     * 更新时间字段 false表示关闭
     * @var false|string
     */
    protected $updateTime = 'updated_time';


    protected $hidden = ["password"];


    public function getInfoByTel($tel, $status = 1) {
        if (empty($tel)) {
            return false;
        }
        $result = $this->where("tel", $tel)->where("status", $status)->find();
        return $result;
    }

    public static function isExistUsername($id = 0, $username = "") : bool {
        if ($id == 0 || $username == "") {
            return false;
        }
        $tmp = static::where([
            ["id", "<>", $id],
            ["username", "=", $username]
        ])->find();

        return $tmp && isset($tmp["id"]) && $tmp["id"] > 0 ? true : false;
    }

    public static function updateById($id, $data) : bool {
        return static::where("id", $id)->save($data);
    }
}
